package cn.edu.scau.cmi.dengyu.client;

import cn.edu.scau.cmi.dengyu.composite.safe.China;
import cn.edu.scau.cmi.dengyu.composite.safe.Composite;
import cn.edu.scau.cmi.dengyu.composite.safe.USA;
import cn.edu.scau.cmi.dengyu.composite.safe.UnitedNation;
import cn.edu.scau.cmi.dengyu.composite.safe.WorldOrganization;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CompositeSafeClient extends Application{
	private String[] countryTypes = {"中国", "美国", "联合国"};
	private ComboBox<String> cbo = new ComboBox<>();
	private static TextArea taDescription = new TextArea();
	Stage stage = new Stage();
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		GridPane pane0 = new GridPane();
		pane0.setAlignment(Pos.CENTER);
		pane0.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
		pane0.setHgap(5.5);
		pane0.setVgap(5.5);

		cbo.setValue(countryTypes[0]);
		ObservableList<String> items = FXCollections.observableArrayList(countryTypes);
		cbo.getItems().addAll(items);

		Button btSure = new Button("显示组织信息");
		btSure.setOnAction(e -> setDisplayText(items.indexOf(cbo.getValue())));

		taDescription.setWrapText(true);
		taDescription.setEditable(false);
		pane0.add(new Label("姓名：邓昱"), 0, 0);
		pane0.add(new Label("学号：201627010204"), 1, 0);
		pane0.add(new Label("请选择组织"), 0, 1);
		pane0.add(cbo, 1, 1);
		pane0.add(btSure, 1, 2);
		GridPane.setHalignment(btSure, HPos.RIGHT);
		
		HBox hBox = new HBox();
		Image image = new Image("cn/edu/scau/cmi/dengyu/client/pic.jpg");
		ImageView imageView = new ImageView(image);
		imageView.setFitHeight(100);
		imageView.setFitWidth(100); 
		imageView.setPreserveRatio(true); 
		hBox.getChildren().add(imageView);
		hBox.getChildren().add(pane0);

		VBox vBox = new VBox();
		vBox.getChildren().add(hBox);
		vBox.getChildren().add(taDescription);

		Scene scene = new Scene(vBox);
		primaryStage.setTitle("实验四 安全型组合模式");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String[] args) {
		Application.launch(args);
	}

	public void setDisplayText(int index) {
		taDescription.setText(selectOrganization(index).say());
	}
	
	public static WorldOrganization selectOrganization(int index) {
		WorldOrganization wo = null;
		switch (index) {
				case 0: wo = new China();break;
				case 1: wo = new USA();break;
				case 2: {
							wo = new UnitedNation(); 
							((UnitedNation) wo).add(new China());
							((UnitedNation) wo).add(new USA());
							break;
						}
		}
		return wo;
	}

	public void showWindow() throws Exception {
		start(stage);				
	}
}
